package campus.aiit.ssl.manage.cron;

import campus.aiit.ssl.service.LocationService;
import campus.aiit.ssl.service.LogService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * cmd(send message) -> socket(write)
 * socket(read) -> cmd(parse resp)
 *
 * @author ihaokun
 * @date 2019/12/20 15:19
 */
@Component
public class AutoDeviceLogCollectTask {
  @Resource private LogService logService;
  @Resource private LocationService locationService;
  int DataCollection = -1;
  @Scheduled(fixedRate = 1000 * 60 * 1)
  private void writeLog(){
      if(new SimpleDateFormat("HH:mm").format(new Date()).equals("00:00")||DataCollection==-1){
          DataCollection = Math.toIntExact(locationService.globalConfig()[0].getDataCollection());
      }
      TimingJudgment timingJudgment = new TimingJudgment();
      if (timingJudgment.CollectOrNot(DataCollection)){

//     String printTime = new SimpleDateFormat("yy-MM-dd HH-mm-ss").format(new Date());
//     System.out.println("PrintWordsJob start at:" + printTime + ", prints: Hello Job-" + new Random().nextInt(100));
//       logService.addDeviceLog();
     logService.writeDeviceLog();
//       System.out.println("Excuse me");
//       System.out.println(new SimpleDateFormat("HH:mm").format(new Date()));
    }
  }
}